package org.xteam.sled.model;

import java.util.List;

import org.xteam.parser.runtime.Span;
import org.xteam.sled.ast.EqOp;
import org.xteam.sled.ast.NeOp;
import org.xteam.sled.ast.Relop;
import org.xteam.sled.semantic.IErrorContext;
import org.xteam.sled.semantic.RangeConstraintsMaker;
import org.xteam.sled.semantic.exp.ExpCondition;

public class AbsoluteProtoPattern extends BasePattern<AbsoluteProtoDisjunct> {

	public AbsoluteProtoPattern(String name, List<AbsoluteProtoDisjunct> disjuncts) {
		super(name, disjuncts);
	}
	
	public AbsoluteProtoPattern() {
		super(null);
	}

	public AbsoluteProtoPattern(String name) {
		super(name);
	}

	public AbsoluteProtoPattern(AbsoluteProtoDisjunct d) {
		super(null, d);
	}

	@Override
	protected BasePattern<AbsoluteProtoDisjunct> createPattern(String name) {
		return new AbsoluteProtoPattern(name);
	}
	
	public static AbsoluteProtoPattern constraint (IErrorContext context, Span span, int len,
			AbsoluteField field, int relop, int offset) {
		AbsoluteProtoPattern result = new AbsoluteProtoPattern();
		for (RangeConstraint<AbsoluteField> c : new RangeConstraintsMaker<AbsoluteField>(
				context, span, field, offset).make(makeRelop(relop))) {
			result.add(new AbsoluteProtoDisjunct(new AbsoluteSequence(c, len)));
		}
		return result;
	}

	private static Relop makeRelop(int relop) {
		switch (relop) {
		case ExpCondition.EQ:
			return new EqOp(new Span(-1, -1));
		case ExpCondition.NE:
			return new NeOp(new Span(-1, -1));
		}
		throw new RuntimeException();
	}

}
